package com.qianfeng.zw.meilishuo.fragment;


import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jayqqaa12.abase.core.fragment.AFragment;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.zw.meilishuo.R;
import com.qianfeng.zw.meilishuo.adapter.FashBannerAdapter;
import com.qianfeng.zw.meilishuo.adapter.FashRecommAdapter;
import com.qianfeng.zw.meilishuo.adapter.FashionGoodsAdapter;
import com.qianfeng.zw.meilishuo.bean.AppConn;
import com.qianfeng.zw.meilishuo.bean.FashBanner;
import com.qianfeng.zw.meilishuo.bean.FashRecom;
import com.qianfeng.zw.meilishuo.bean.TwitterGoods;
import com.qianfeng.zw.meilishuo.config.AppConstants;
import com.qianfeng.zw.meilishuo.widget.AutoScrollViewPager;
import com.qianfeng.zw.meilishuo.widget.GridViewForScrollView;
import com.qianfeng.zw.meilishuo.widget.SwipeRefreshLayout;

import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 * 流行
 */
@EFragment(R.layout.fragment_fashion)
public class FashionFragment1 extends AFragment implements SwipeRefreshLayout.OnRefreshListener, SwipeRefreshLayout.OnLoadListener, ViewPager.OnPageChangeListener {
    private HttpHandler<String> remmHandler, bannerHandler;
//    @ViewById(R.id.fragment_fashion_swiprefresh)
    SwipeRefreshLayout swipeRefreshLayout;
    /**
     * 广告栏
     */
    @ViewById(R.id.fragment_fashion_banner_autoviewpager)
    AutoScrollViewPager bannerAutoVp;
    private FashBannerAdapter fashBannerAdapter;
    private ArrayList<String> imageUrls = new ArrayList<String>();
    @ViewById(R.id.fragment_fashion_dot_ll)
    LinearLayout dotLayout;
    /**
     * 推荐
     */
    @ViewById(R.id.fragment_fashion_recommend_gridview)
    GridViewForScrollView recommendGridView;
    private FashRecommAdapter remmAdapter;
    private ArrayList<FashRecom> fashRemms = new ArrayList<>();
    private ArrayList<FashRecom> fashRemmitemList = new ArrayList<>();
    @ViewById(R.id.fragment_fashion_hide_btn)
    ToggleButton tBtn;
    /**
     * 买手圈
     */
//    @ViewById(R.id.fragment_fashion_promote_recyclerview)
//    RecyclerView promoteRecyclerView;
//    private FashPromoteAdapter promoteAdapter;
//    private ArrayList<FashPromote> fashPromotes = new ArrayList<FashPromote>();

    /**
     * 商品列表
     */
//    @ViewById(R.id.fragment_fashion_recyclerView)
    RecyclerView recyclerView;

    FashionGoodsAdapter goodsAdapter;

    ArrayList<TwitterGoods> goods = new ArrayList<TwitterGoods>();

    //    @ViewById(R.id.loading_view)
//    LinearLayout loading;

    /**
     * 程序入口
     */
    @Override
    public void init() {
        initSwipeRefresh();
        initBannerView();
        initRecommendView();
        initBannerView();
        initPromoteView();
        initGoodsView();
    }

    private void initSwipeRefresh() {
        swipeRefreshLayout.setMode(SwipeRefreshLayout.Mode.BOTH);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setOnLoadListener(this);
        swipeRefreshLayout.setLoadNoFull(true);
    }

    /**
     * 初始化首页横幅
     */
    private void initBannerView() {
        fashBannerAdapter = new FashBannerAdapter(activity, dotLayout, imageUrls);
        bannerAutoVp.setAdapter(fashBannerAdapter);
        bannerAutoVp.setOnPageChangeListener(fashBannerAdapter);
    }

    /**
     * 初始化推荐控件
     */
    private void initRecommendView() {
        remmAdapter = new FashRecommAdapter(fashRemmitemList, activity);
        recommendGridView.setAdapter(remmAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        bannerAutoVp.startAutoScroll();
    }

    @Override
    public void onPause() {
        super.onPause();
        bannerAutoVp.stopAutoScroll();
    }

    /**
     * 初始化热门买手圈
     */
    private void initPromoteView() {
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity, LinearLayoutManager.HORIZONTAL,false);
//        promoteRecyclerView.setLayoutManager(linearLayoutManager);
//        promoteAdapter = new FashPromoteAdapter(fashPromotes);
//        promoteRecyclerView.setAdapter(promoteAdapter);
    }

    private void initGoodsView() {
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        goodsAdapter = new FashionGoodsAdapter(goods);
        recyclerView.setAdapter(goodsAdapter);
    }

    /**
     *
     */
    @Override
    public void connect() {
        //加载广告栏数据
        loadBannerData();
        //加载推荐分类数据
        loadRecommData();
        //加载热门买手圈数据
//        httpHelper.loadGoodsData(HttpRequest.HttpMethod.GET, AppConstants.HOME_FASHION_PROMOTE_URL, fashPromotes, promoteAdapter, new TypeReference<AppConn<FashPromote>>() {
//        });
        //加载主页商品信息数据
        httpHelper.loadGoodsData(HttpRequest.HttpMethod.GET, AppConstants.HOME_FASHION_GET_GOODS, goods, goodsAdapter, new TypeReference<AppConn<TwitterGoods>>() {
        });
    }

    @CheckedChange(R.id.fragment_fashion_hide_btn)
    public void setOnCheckedChangeListener(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            remmAdapter.setIsShowAllView(fashRemms, recommendGridView);
        } else {
            remmAdapter.setIsShowAllView(fashRemmitemList, recommendGridView);
        }

    }

    private void loadBannerData() {
        bannerHandler = new HttpUtils().send(HttpRequest.HttpMethod.GET, AppConstants.HOME_FASHION_BANNER_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn appComm = JSONObject.parseObject(responseInfo.result, new TypeReference<AppConn<FashBanner>>() {
                });
                if (appComm != null) {
                    ArrayList<FashBanner> banners = (ArrayList<FashBanner>) appComm.getData();
                    if (banners != null && !banners.isEmpty()) {
                        for (int i = 0; i < banners.size(); i++) {
                            imageUrls.add(banners.get(i).getImageUrl());
                        }
                        fashBannerAdapter.refreshView();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /**
     * 加载人们推荐数据
     */
    private void loadRecommData() {
        remmHandler = new HttpUtils().send(HttpRequest.HttpMethod.GET, AppConstants.HOME_FASHION_RECOMMEND_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                AppConn conn = JSONObject.parseObject(responseInfo.result, new TypeReference<AppConn<FashRecom>>() {
                });
                if (conn != null) {
                    fashRemms = (ArrayList<FashRecom>) conn.getData();
                    for (int i = 0; i < 4; i++) {
                        fashRemmitemList.add(fashRemms.get(i));
                    }
                    remmAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        remmHandler.cancel();
        bannerHandler.cancel();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {

    }

    /**
     * 上拉加载更多
     */
    @Override
    public void onLoad() {

    }
}
